package com.hxxy.wyp.petsLover.common.services.base.backend.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxxy.wyp.petsLover.common.entities.*;
import com.hxxy.wyp.petsLover.common.entities.vo.req.EvalutionReq;
import com.hxxy.wyp.petsLover.common.entities.vo.req.PayVo;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.OrderEvolutionVo;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.OrdersVo;
import com.hxxy.wyp.petsLover.common.mapper.EvalutionMapper;
import com.hxxy.wyp.petsLover.common.mapper.GoodMapper;
import com.hxxy.wyp.petsLover.common.mapper.OrdersMapper;
import com.hxxy.wyp.petsLover.common.mapper.ShopCartMapper;
import com.hxxy.wyp.petsLover.common.services.base.backend.OrderService;
import com.hxxy.wyp.petsLover.security.SecurityContextFrontUser;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName OrderServiceImpl
 * @Description TODO
 * @Author wyp
 * @Date 2023/1/29 15:20
 * @Version 1.0
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrderService {
    @Autowired
    SecurityContextFrontUser securityContextFrontUser;

    @Resource
    ShopCartMapper shopCartMapper;

    @Resource
    OrdersMapper ordersMapper;

    @Resource
    GoodMapper goodMapper;

    @Resource
    EvalutionMapper evalutionMapper;


    @Override
    public Page pageSearch(Page pageInfo, LambdaQueryWrapper<Orders> lambdaQueryWrapper) {
        return baseMapper.selectPage(pageInfo, lambdaQueryWrapper);
    }

    @Override
    public List<OrdersVo> selectOrderListByUser() {
        User userInfo = securityContextFrontUser.getUserInfo();
        List<Orders> orders = ordersMapper.selectList(
                new LambdaQueryWrapper<Orders>()
                        .eq(userInfo.getId() != null, Orders::getUserId, userInfo.getId()));
        return orders.stream().map(order -> {
            OrdersVo ordersVo = new OrdersVo();
            BeanUtils.copyProperties(order, ordersVo);
            List<String> shopCartIds = Arrays.stream(order.getShopCartId().split(",")).collect(Collectors.toList());
            List<ShopCart> shopCarts = shopCartIds.stream().map(id -> {
                LambdaQueryWrapper<Good> goodLambdaQueryWrapper = new LambdaQueryWrapper<>();
                ShopCart shopCart = shopCartMapper.selectById(id);
                Good good = goodMapper.selectOne(goodLambdaQueryWrapper.eq(Good::getId, shopCart.getGoodId()));
                shopCart.setGood(good);
                return shopCart;
            }).collect(Collectors.toList());
            ordersVo.setShopCartList(shopCarts);
            return ordersVo;
        }).collect(Collectors.toList());
    }

    @Override
    public OrdersVo selectOrderInfoMore(String id) {
        OrdersVo ordersVo = new OrdersVo();
        Orders orders = ordersMapper.selectById(id);
        BeanUtils.copyProperties(orders, ordersVo);
        List<String> shopCartIds = Arrays.stream(orders.getShopCartId().split(",")).collect(Collectors.toList());
        List<ShopCart> shopCarts = shopCartIds.stream().map((shopCartId) -> {
            LambdaQueryWrapper<Good> goodLambdaQueryWrapper = new LambdaQueryWrapper<>();
            ShopCart shopCart = shopCartMapper.selectById(shopCartId);
            Good good = goodMapper.selectOne(goodLambdaQueryWrapper.eq(Good::getId, shopCart.getGoodId()));
            String[] split = good.getPic().split(",");
            List<String> picList = Arrays.asList(split);
            good.setPic(picList.get(0));
            good.setPicList(picList);
            shopCart.setGood(good);
            return shopCart;
        }).collect(Collectors.toList());
        ordersVo.setShopCartList(shopCarts);
        return ordersVo;
    }

    @Override
    public void evaluatedCommodity(EvalutionReq evalutionReq) {
        Evaluation evaluation = new Evaluation();
        User userInfo = securityContextFrontUser.getUserInfo();
        evaluation.setEvaluationOrderId(Long.parseLong(evalutionReq.getEvalutionOrderId()));
        evaluation.setEvaluationContent(evalutionReq.getEvalutionContent());
        evaluation.setEvaluationUserId(userInfo.getId());
        evaluation.setEvaluationTime(LocalDateTime.now());
        evalutionMapper.insert(evaluation);
        //更新订单评价状态
        Orders orders = ordersMapper.selectById(evalutionReq.getEvalutionOrderId());
        orders.setIsEvoluted("1");
        ordersMapper.updateById(orders);
    }

    @Override
    public OrderEvolutionVo selectAllOrderEvolutions() {
        OrderEvolutionVo orderEvolutionVo = new OrderEvolutionVo();
        List<Evaluation> evaluations = evalutionMapper.selectList(new LambdaQueryWrapper<>(Evaluation.class));
        Collections.synchronizedList(evaluations);
        evaluations.forEach(evaluation -> {
            OrdersVo ordersVo = selectOrderInfoMore(evaluation.getEvaluationOrderId().toString());
            evaluation.setOrdersVo(ordersVo);
        });
        orderEvolutionVo.setEvaluations(evaluations);
        return orderEvolutionVo;
    }

    @Override
    public PayVo getOrderPay(String orderSn) {

        PayVo payVo = new PayVo();
        Orders orders = ordersMapper.selectOne(new LambdaQueryWrapper<>(Orders.class).eq(Orders::getOrderId, orderSn));
        payVo.setBody("备注");
        payVo.setSubject("结算");
        payVo.setTotal_amount(orders.getOrderMoney().toString());
        payVo.setOut_trade_no(orders.getOrderId().toString());

        return payVo;

    }
}
